package com.example.web;

import com.example.pojo.User;
import com.example.service.UserService;
import com.example.service.impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {

    private UserService userService = new UserServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if(userService.login(new User(null, username, password, null)) == null){
            //把错误信息和回显的表单项放进Request域中
            request.setAttribute("msg", "用户名或密码错误！");
            request.setAttribute("username", username);
            //登录失败跳回登录界面
            request.getRequestDispatcher("/pages/user/login.jsp").forward(request, response);
        }else{
            //登录成功
            request.getRequestDispatcher("/pages/user/login_success.jsp").forward(request, response);
        }
    }
}
